import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { appStore } from "../store/index";

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        component: () => import('../views/Login.vue'),
    },
    {
        path: '/home',
        component: () => import('../home/home.vue'),
        children: [
            { path: '' ,component:() => import('../home/page/meter.vue'),meta:{
                title:'仪表盘'
            }},
            { path: '/focus' ,component:() => import('../home/page/focus.vue'),meta:{
                title:'焦点图管理'
            }},
            { path: '/enterprise' ,component:() => import('../home/page/enterprise.vue'),meta:{
                title:'企业特色管理'
            }},
            { path: '/addbanner' ,component:() => import('../home/page/addbanner.vue'),meta:{
                title:'添加焦点图'
            }},{ path: '/editbanner/:id' ,component:() => import('../home/page/editbanner.vue'),meta:{
                title:'修改焦点图'
            }},{ path: '/addenter' ,component:() => import('../home/page/addenter.vue'),meta:{
                title:'添加首页公司简介'
            }},{ path: '/editenter/:id' ,component:() => import('../home/page/editenter.vue'),meta:{
                title:'修改首页公司简介'
            }},{ path: '/aboutuser' ,component:() => import('../home/page/aboutuser.vue'),meta:{
                title:'关于我们'
            }},{ path: '/administration' ,component:() => import('../home/product/Administration.vue'),meta:{
                title:'产品管理'
            }},{ path: '/productclass' ,component:() => import('../home/product/productclass.vue'),meta:{
                title:'分类管理'
            }},{ path: '/strators' ,component:() => import('../home/strators/strators.vue'),meta:{
                title:'管理员'
            }},{ path: '/addstrators' ,component:() => import('../home/strators/addstrators.vue'),meta:{
                title:'添加管理员'
            }},{ path: '/productadd' ,component:() => import('../home/product/productadd.vue'),meta:{
                title:'添加管理员'
            }},
            
            
            
        ]
    }, {
        path: '/',
        redirect: '/login'
    }

]

const router = createRouter({
    history: createWebHistory(),
    routes
})



router.beforeEach((to, form, next) => {
    const store = appStore()
    const token = store.$state.access_token
    if (token && to.path === '/login') {
        return next('/home')
    }
    if (!token && to.path != '/login') {
        return next('/login')
    }

    store.TITLE(to.meta.title)
    

    next()
    // from来自哪里
    //to 去哪里

})
export default router